$(function(){

	if (window.top!=window.self){
		window.top.location.href = '/index.php/Admin/Login/';
	}
	
	
	var $name 	= $('#name');
	$name.focus();

    var $passwd = $('#passwd');

    var $mobile_phone   = $('#mobile_phone');
    var $verify_code    = $('#verify_code');
	
	var $btnLogin = $('#btnLogin');
	$btnLogin.click(function(){
		Login($.trim($name.val()),$.trim($passwd.val()),$.trim($mobile_phone.val()),$.trim($verify_code.val()));
	});

	$passwd.keydown(function(event){
		  if(event.keyCode == 13) {
			  $btnLogin.click();
		  }
	});

    //发送验证码
    var $btnVerifyCode = $('#btnVerifyCode');
    $btnVerifyCode.click(function(){

        var mobile_phone = $.trim($('#mobile_phone').val());

        if(mobile_phone == ''){
            alert('请填写手机号');
            return false;
        }

        if(mobile_phone.length != 11){
            alert('手机号必须为11位数字');
            return false;
        }

        $btnVerifyCode.attr('disabled',true).val('正在发送中...');
        SendVerifyCode(mobile_phone);
    });


});

//登录
function Login(name,passwd,mobile_phone,verify_code){
	var url = '/index.php/Admin/Login/login_do';
	var params = {
		'name':name,
        'passwd':passwd,
        'mobile_phone':mobile_phone,
        'verify_code':verify_code
	};
	
	$.post(url,params,function(data){
		if(data.success){
			location.href = '/index.php/Admin/Index/';
		}else{
			alert(data.message);
		}
	},'json');
}

//验证码
function SendVerifyCode(mobile_phone){

    var url = '/index.php/Admin/Login/send_sms_verify_code';
    var params = {
        'mobile_phone':mobile_phone
    };
    $.post(url,params,function(data){
        if(data.success){
            alert(data.message);
            btnVerifyCountDown();
        }else{
            alert(data.message);
            $('#btnVerifyCode').attr('disabled',false).val('获取短信验证码');
        }
    },'json');
}

//倒计时
function btnVerifyCountDown(){

    var time = 60;
    var $btnVerifyCode = $('#btnVerifyCode');
    $btnVerifyCode.attr('disabled',true);
    var timeInt = setInterval(function(){
        if(time > 0){
            $btnVerifyCode.val(time + '秒后可重新发送');
        }else{
            clearInterval(timeInt);
            $('#btnVerifyCode').attr('disabled',false).val('获取短信验证码');
        }
        time--
    },1000);

}